package com.yx.cloud.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import com.yx.cloud.R;
import com.yx.cloud.product.sdk.tmall.MusicInfo;
import com.yx.cloud.utils.RomUtils;
import com.yx.cloud.view.ProgressBarView;
import java.util.List;

public class MusicAlbumDetailListAdapter extends BaseAdapter implements View.OnClickListener{
    private static final String TAG = "MusicListAdapter";
    private Context context;
    private List<MusicInfo> mMusicInfoList;
    private MusicAlbumDetailListAdapter.InnerItemOnclickListener listener;
    private int currentPos = -1;
    private int musicPlaying = -1;
    private boolean changePlayBtnShow = false;

    public MusicAlbumDetailListAdapter(Context context, List<MusicInfo> musicInfoList) {
        this.context = context;
        this.mMusicInfoList = musicInfoList;
    }

    public void  updateData(List<MusicInfo> musicInfoList){
        this.mMusicInfoList = musicInfoList;
        notifyDataSetChanged();
    }

    public void  selectItem(int position){
        currentPos = position;
        notifyDataSetChanged();
    }

    public int getSelectedItem(){
        return currentPos;
    }

    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        changePlayBtnShow = true;
        currentPos = (int)v.getTag();
        listener.itemClick(v,(int)v.getTag());
    }

    private void changePlayBtnShow(MusicAlbumDetailListAdapter.MyViewHolder viewHolder,MusicInfo musicInfo){
        if(musicInfo.getMusicPlayStatus() == MusicInfo.STATE_PAUSE){
            Log.d("music","set music playing");
            musicInfo.setMusicPlayStatus(MusicInfo.STATE_PLAY);
            viewHolder.ivMusicPlay.setBackgroundResource(R.mipmap.music_playing);
            viewHolder.progressBarView.setProgress(musicInfo.getProgress());
            musicPlaying = 1;
            //播放完毕
            if(musicInfo.getProgress() == 100){
                viewHolder.ivMusicPlay.setBackgroundResource(R.mipmap.music_stop);
                musicInfo.setMusicPlayStatus(MusicInfo.STATE_IDLE);
                viewHolder.progressBarView.setProgress(0);
                musicPlaying = -1;
            }
        }else if(musicInfo.getMusicPlayStatus().equals(MusicInfo.STATE_PLAY)){
            Log.d("music","set music stop");
            musicInfo.setMusicPlayStatus(MusicInfo.STATE_PAUSE);
            viewHolder.ivMusicPlay.setBackgroundResource(R.mipmap.music_stop);
            viewHolder.progressBarView.setProgress(0);
            musicPlaying = 0;
        }else if(musicInfo.getMusicPlayStatus().equals(MusicInfo.STATE_IDLE)){
            Log.d("music","set music playing");
            musicInfo.setMusicPlayStatus(MusicInfo.STATE_PLAY);
            viewHolder.ivMusicPlay.setBackgroundResource(R.mipmap.music_playing);
            viewHolder.progressBarView.setProgress(musicInfo.getProgress());
            musicPlaying = 1;
        }
    }

    private void refreshPlayBtnShow(MusicAlbumDetailListAdapter.MyViewHolder viewHolder,MusicInfo musicInfo){
        if(musicInfo.getMusicPlayStatus().equals(MusicInfo.STATE_PAUSE)){
            Log.d("music","set music stop");
            viewHolder.ivMusicPlay.setBackgroundResource(R.mipmap.music_stop);
            viewHolder.progressBarView.setProgress(0);
            musicPlaying = 0;
        }else if(musicInfo.getMusicPlayStatus().equals(MusicInfo.STATE_PLAY)){
            Log.d("music","set music playing");
            viewHolder.ivMusicPlay.setBackgroundResource(R.mipmap.music_playing);
            viewHolder.progressBarView.setProgress(musicInfo.getProgress());
            musicPlaying = 1;
            //播放完毕
            if(musicInfo.getProgress() == 100){
                viewHolder.ivMusicPlay.setBackgroundResource(R.mipmap.music_stop);
                musicInfo.setMusicPlayStatus(MusicInfo.STATE_IDLE);
                viewHolder.progressBarView.setProgress(0);
                musicPlaying = -1;
            }
        }else {
            Log.d("music","reset music");
            viewHolder.ivMusicPlay.setBackgroundResource(R.mipmap.music_stop);
            viewHolder.progressBarView.setProgress(0);
            musicPlaying = -1;
        }
    }

    public int getItemCount() {
        return mMusicInfoList.size();
    }

    public void updateMusic(List<MusicInfo> musicInfoList) {
        mMusicInfoList = musicInfoList;
        notifyDataSetChanged();
    }

    public void addMusic(List<MusicInfo> musicInfoList) {
        if(musicInfoList == null){
            return;
        }
        mMusicInfoList.addAll(musicInfoList);
        notifyDataSetChanged();
    }

    public List<MusicInfo> getmMusicInfoList() {
        return mMusicInfoList;
    }

    public int getMusicPlaying() {
        return musicPlaying;
    }

    public void setMusicPlaying(int musicPlaying) {
        this.musicPlaying = musicPlaying;
    }

    public boolean isChangePlayBtnShow() {
        return changePlayBtnShow;
    }

    public void setChangePlayBtnShow(boolean changePlayBtnShow) {
        this.changePlayBtnShow = changePlayBtnShow;
    }

    @Override
    public int getCount() {
        return mMusicInfoList.size();
    }

    @Override
    public MusicInfo getItem(int position) {
        return mMusicInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public int getCurrentPos() {
        return currentPos;
    }

    public void setCurrentPos(int currentPos) {
        this.currentPos = currentPos;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MusicAlbumDetailListAdapter.MyViewHolder myViewHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.music_list_item, null);
            myViewHolder = new MusicAlbumDetailListAdapter.MyViewHolder();
            myViewHolder.flMusicPlay = convertView.findViewById(R.id.fl_play);
            myViewHolder.progressBarView = convertView.findViewById(R.id.progress_view);
            myViewHolder.tvCount = convertView.findViewById(R.id.tv_music_item_count);
            myViewHolder.tvMusicName = convertView.findViewById(R.id.tv_music_name);
            myViewHolder.tvSinger = convertView.findViewById(R.id.tv_singer);
            myViewHolder.ivMusicPlay = convertView.findViewById(R.id.iv_play);
            convertView.setTag(myViewHolder);
        } else {
            myViewHolder = (MusicAlbumDetailListAdapter.MyViewHolder) convertView.getTag();
        }

        MusicInfo musicInfo = mMusicInfoList.get(position);
        myViewHolder.flMusicPlay.setOnClickListener(this);
        myViewHolder.flMusicPlay.setTag(position);

        if(currentPos == position){
            if(changePlayBtnShow){
                changePlayBtnShow(myViewHolder,musicInfo);
                changePlayBtnShow = false;
            }else {
                refreshPlayBtnShow(myViewHolder,musicInfo);
            }
        }else {
            myViewHolder.ivMusicPlay.setBackgroundResource(R.mipmap.music_stop);
            musicInfo.setMusicPlayStatus(MusicInfo.STATE_IDLE);
            myViewHolder.progressBarView.setProgress(0);
        }

        myViewHolder.tvCount.setText(position + 1 + ".");

        myViewHolder.tvMusicName.setText(musicInfo.getTitle());
        if(musicInfo.getSingers() == null ||musicInfo.getSingers().equals("")){
            myViewHolder.tvSinger.setVisibility(View.GONE);
        }else {
            myViewHolder.tvSinger.setVisibility(View.VISIBLE);
            myViewHolder.tvSinger.setText(musicInfo.getSingers());
        }
        Log.d("music","play position + " + position);
        return convertView;
    }

    public interface InnerItemOnclickListener {
        void itemClick(View v,int position);
    }

    public void setOnInnerItemOnClickListener(MusicAlbumDetailListAdapter.InnerItemOnclickListener listener) {
        this.listener = listener;
    }

    class MyViewHolder{
        FrameLayout flMusicPlay;
        TextView tvCount;
        TextView tvMusicName;
        TextView tvSinger;
        ProgressBarView progressBarView;
        ImageView ivMusicPlay;
    }
}
